package com.evil.kingdom.pojo.entity.config;

import com.alibaba.fastjson.JSON;
import com.evil.common.mongo.entity.SingleName;
import com.evil.kingdom.pojo.enums.ConfigEnum;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
 * 系统配置
 *
 * @author liyang
 * @date 2024-06-02 18:59
 */
@Setter
@Getter
@Document
@NoArgsConstructor
@AllArgsConstructor
public class Config implements SingleName {

    /**
     * 名称
     */
    @Id
    @NotBlank(message = "名称不能为空")
    private String name;

    /**
     * 值
     */
    private String value;

    public <T extends ConfigValue> Config(T t) {
        ConfigEnum configEnum = ConfigEnum.findByValueClass(t.getClass());
        this.name = configEnum.name();
        this.value = JSON.toJSONString(t);
    }

    @Override
    public String getEntityName() {
        return "系统配置";
    }
}
